package fz.vrd.ui.toast;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import fz.vrd.ui.R;

class ToastUtils {

    private static Toast currentToast;

    @SuppressLint("StaticFieldLeak")
    private static ToastUtils toastUtils;

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;

    public static ToastUtils getInstance() {
        if (toastUtils == null) {
            toastUtils = new ToastUtils();
        }
        return toastUtils;
    }

    public void build(Context context) {
        if (null == mContext) {
            mContext = context;
        }
        if (null == currentToast) {
            currentToast = new Toast(mContext);
        }
    }

    public static void onSuccessShowToast(String message) {
        showToast(message, R.mipmap.lib_ui_toast_success,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_success_shape);
    }

    public static void onSuccessShowToast(String message,@ColorRes int iconID) {
        showToast(message, iconID,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_success_shape);
    }

    public static void onErrorShowToast(String message) {
        showToast(message, R.mipmap.lib_ui_toast_error,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_error_shape);
    }


    public static void onErrorShowToast(String message,@ColorRes int iconID) {
        showToast(message, iconID,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_error_shape);
    }


    public static void onDefaultShowToast(String message) {
        showToast(message, R.mipmap.lib_ui_toast_default,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_default_shape);
    }

    public static void onDefaultShowToast(String message,@ColorRes int iconID) {
        showToast(message, iconID,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_default_shape);
    }

    public static void onDefaultWithoutIconShowToast(String message) {
        showToast(message, 0,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_default_shape);
    }

    public static void onWarnShowToast(String message) {
        showToast(message, R.mipmap.lib_ui_toast_warn,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_warn_shape);
    }

    public static void onWarnShowToast(String message,@ColorRes int iconID) {
        showToast(message, iconID,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_warn_shape);
    }


    public static void onInfoShowToast(String message) {
        showToast(message, R.mipmap.lib_ui_toast_info,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_info_shape);
    }

    public static void onInfoShowToast(String message, int iconID) {
        showToast(message, iconID,
                R.color.lib_ui_toast_default_text_color,
                Toast.LENGTH_SHORT, R.drawable.lib_ui_toast_info_shape);
    }

    public static void showToast(String message, @DrawableRes int iconID,
                                 @ColorRes int textColor, int duration, @DrawableRes int toastDrawableID) {
        showToast_(message, iconID, textColor, duration, toastDrawableID);
    }

    /**
     * @param message         ： 提示内容
     * @param iconID          ：图标id
     * @param textColor       ：字体颜色
     * @param duration        : 显示时间
     * @param toastDrawableID ： 背景颜色
     */
    private static void showToast_(String message, @DrawableRes int iconID, @ColorRes int textColor, int duration, @DrawableRes int toastDrawableID) {

        if (currentToast == null) {
            currentToast = new Toast(mContext);
        }

        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(mContext).inflate(R.layout.lib_ui_toast_view, null);
        RelativeLayout toast_container = view.findViewById(R.id.toast_container);
        ImageView toast_icon = view.findViewById(R.id.toast_icon);
        TextView toast_message = view.findViewById(R.id.toast_message);

        //左边的icon
        if (iconID != 0) {
            toast_icon.setVisibility(View.VISIBLE);
            toast_icon.setImageDrawable(ContextCompat.getDrawable(mContext, iconID));
            toast_message.setPadding(dp2px(10), 0, 0, 0);
        } else {
            toast_icon.setVisibility(View.GONE);
            toast_message.setPadding(0, 0, 0, 0);
        }
        //右边的message
        toast_message.setText(message);
        if (0 != textColor) {
            toast_message.setTextColor(ContextCompat.getColor(mContext, textColor));
        } else {
            toast_message.setTextColor(ContextCompat.getColor(mContext, R.color.lib_ui_toast_default_text_color));
        }
        //背景样式
        if (0 != toastDrawableID) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                toast_container.setBackground(ContextCompat.getDrawable(mContext, toastDrawableID));
            } else {
                toast_container.setBackgroundResource(toastDrawableID);
            }
        }

        toast_message.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
        currentToast.setView(view);
        currentToast.setDuration(duration);
        currentToast.show();

    }

    private static int dp2px(float dpValue) {
        float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5F);
    }

}
